
//规则条件列表js代码块
var SysUserListPage = {
		
		//初始化一些东西
		init : function(){
			
			 //初始化角色选择下来列表
			 this.initRoleChooseSelect();
		},
		
		//初始化角色选择下来类表
		initRoleChooseSelect : function(){
			
			var requestUrl = "/sysRole/findSysRoleList";
			
			var params = {"pageNum" : 1 , "pageSize" : 100000};
			
			var txt = "";
			
			ajaxRequestByJSONParams(requestUrl , params , function(rst){
				
				if(rst.code != 200){
					new $.zui.Messager(data.message, {icon:'times',type: 'danger',placement: 'center',time:'2000'}).show();
					return;
				}
				
				var data = rst.data.list;
				
				for(var i = 0 ; i < data.length ; i ++){
					
					var item = data[i];
					
					txt += '<option value="'+item.id+'">'+item.roleName+'</option>';
					
				}
				
				//添加option并初始化
				$('.roleSelect').html(txt).chosen({
				    no_results_text: '没有找到',    // 当检索时没有找到匹配项时显示的提示文本
				    disable_search_threshold: 10, // 10 个以下的选择项则不显示检索框
				    search_contains: true         // 从任意位置开始检索
				});
				
				//不设置无法展示
				$(".chosen-container").css({"width":"100%"});
				
			});
			
		},
		
		 /*创建分页实例current*/
	    pagingTooler : new PagingTooler({
	        callbackMethodName : "SysUserListPage.findSysUserList",
	        pageSize : 10,
	        firstPageBtnID : "firstPageBtn",
	        lastPageBtnID : "lastPageBtn",
	        pageCodeLayerID : "pageCodeLayer",
	        recordListLayerID : "dataListLayer",
	        showCurrentPageLayerID : "currentPageLayer",
	        showRecordCountLayerID : "recordCountLayer",
	        showPageCountLayerID : "pageCountLayer",
	        showLoadingLayerID : "loadingLayer",
	        showNoRecordLayerID : "recordLayer",
	        pagingToolbarLayerID : "pagingToolbarLayer",
	        pageSizeSelectLayerID : "pageSizeSelectLayer",
	        jumpPageCodeInputID : "pageInput",
	        jumpPageBtnID : "goPageBtn",
	        pageSizeArray : [10,15,20],
	        hideColumnLayerID : "hideColumnLayer",   //隐藏列div的ID
	    }),
	    
	    search : function(){
	    	var forms = $(".searchParameter");
			var params = {};
			for(var i = 0 ; i < forms.length; i ++){
				var form = $(forms[i]);
				var id = form.attr("id");
				id = id.replace("search_","");
				var value = $.trim(form.val());
				if(value == "") continue;
				params[id] = value;
			}
			
			this.pagingTooler.search(params);
			
	    },
	    
	    findSysUserList : function(){
	    	
	    	var params = this.pagingTooler.getPagingAndSearchParams();
	    	
	     	$.ajax({
			 	url : '/sysUser/findSysUserWithRoleInfoList',
			 	type :'POST',
			 	data : JSON.stringify(params),
			 	dataType : "json",
			 	contentType : 'application/json; charset=UTF-8',
			 	success:function(data){
			 		var count = data.data.total;
			 		var dataList = data.data.list;
			 		var txt = "";
			 		SysUserListPage.dataList = dataList;
			 		for(var i = 0 ; dataList != null && i < dataList.length ; i ++){
			 			txt +=SysUserListPage.createRecordTxt(dataList[i], i)
			 		}
			 		SysUserListPage.pagingTooler.showInPage(count, txt,"currentNumberPage");
			 	}		
			 });

	    },
	    
	    //创建表格中每一条记录的文本
	    createRecordTxt : function(data , index){
	    	
	    	var txt = "";
	    	
	    	var roleInfoList = data.roleInfoList ? data.roleInfoList : [];
	    	
	    	var roleNames  = [];
	    	
	    	for(var i = 0 ; i < roleInfoList.length ; i ++){
	    		roleNames.push(roleInfoList[i].roleName);
	    	}
	    	
			txt = '<tr>'+
						'<td><input type="checkbox" class="checkitem"/></td>'+
						'<td>'+CommonUtil.fillBlank(data.account)+'</td>'+
						'<td>'+CommonUtil.fillBlank(data.userName)+'</td>'+
						'<td>'+CommonUtil.fillBlank(roleNames.join(" , "))+'</td>'+
						'<td>'+CommonUtil.fillBlank(data.gender)+'</td>'+
						'<td>'+CommonUtil.fillBlank(data.phoneNumber)+'</td>'+
						'<td>'+CommonUtil.fillBlank(data.email)+'</td>'+
						'<td>'+CommonUtil.fillBlank(data.enable == 1 ? "启用" : "禁用")+'</td>'+
	    				'<td>'+
		    				'<button onclick="SysUserListPage.popDetailSysUserWindow('+index+')"  class="btn btn-mini">查看</button>&nbsp;'+
		    				'<button onclick="SysUserListPage.popEditSysUserWindow('+index+')"  class="btn btn-mini permissionMenu editUserBtn">编辑</button>&nbsp;'+
		    				'<button onclick="SysUserListPage.resetPasswordSysUserWindow('+index+')"  class="btn btn-mini permissionMenu resetPasswordBtn">重置密码</button>&nbsp;'+
	    					'<button onclick="SysUserListPage.popDeleteSysUserWindow('+index+')"  class="btn btn-mini permissionMenu deleteUserBtn">删除</button>&nbsp;'+
    					'</td>'+
	    			  '</tr>';
	    	return txt;
	    },
	    
	    //弹出新增窗口
	    popEditSysUserWindow : function(index){
	     	
	     	//当前编辑对象
	    	this.currentUpdateRecord = this.dataList[index];
	    	
	    	var currentUpdateRecord = this.currentUpdateRecord;
	    	
	    	//显示弹窗
	     	$('#editSysUserWindow').attr("editId" , currentUpdateRecord.id).modal();
	     	
	     	for(var key in currentUpdateRecord){
	     		
	     		var value = currentUpdateRecord[key];
	     		
	     		if(key == "birthday" && value && value != "" && value != null){
	     			$("#edit_" + key).val(CommonUtil.getDateYmd(new Date(value)));
	     		}else{
	     			$("#edit_" + key).val(value);
	     		}
	     		
	     	}
	     	
	    	var roleInfoList = currentUpdateRecord.roleInfoList ? currentUpdateRecord.roleInfoList : [];
	    	
	    	var roleIds  = [];
	    	
	    	for(var i = 0 ; i < roleInfoList.length ; i ++){
	    		roleIds.push(roleInfoList[i].id);
	    	}
	    	
	    	//回显
	    	 $("#editSysUserWindow").find(".roleSelect").val(roleIds).trigger('chosen:updated');
	    },
	    
	  //获取新增的表单参数
      getUpdateSysUserFormParams : function(){
	    	
	    	var params = {};
	    	
			params.account =  $.trim($("#edit_account").val());
			params.userName =  $.trim($("#edit_userName").val());
			params.email =  $.trim($("#edit_email").val());
			params.phoneNumber =  $.trim($("#edit_phoneNumber").val());
			params.gender =  $.trim($("#edit_gender").val());
			params.birthday =  $.trim($("#edit_birthday").val());
			params.enable =  $.trim($("#edit_enable").val());
	    	params.id = $('#editSysUserWindow').attr("editId");
	    	params.roleIds = $("#editSysUserWindow").find(".roleSelect").val();
			
	    	return params;
	    	
	    },
	    
	    //校验新增的参数
	    validateUpdateSysUserFormParams : function(params){
	    	
	    	if(params.userName == ""){
	    		new $.zui.Messager('姓名不能为空', {icon:'times',type: 'danger',placement: 'center',time:'2000'}).show();
	    		return;
	    	}
	    	
	    	//校验的逻辑自己填写,返回true和false,可以弹出一些提示
	    	return true;

	    },
	    
	    //新增
	    updateSysUser : function(){
	    	
	    	var params = this.getUpdateSysUserFormParams();
	    	
	    	//校验不通过,直接返回
	    	if(!this.validateUpdateSysUserFormParams(params)) return;
	    	
	    	$.ajax({
			 	url : '/sysUser/updateSysUser',
			 	type :'POST',
			 	data : JSON.stringify(params),
			 	
			 	beforeSend : function(){
			 		$("#addSysUserBtn").attr("disabled","disabled");
			 	},
			 	complete: function(){
			 		$("#addSysUserBtn").removeAttr("disabled","disabled");
			 	},
			 	dataType : "json",
			 	contentType : 'application/json; charset=UTF-8',
			 	success:function(data){
			 		
			 		if(data.code == 200){
			 			new $.zui.Messager('修改成功!', {icon:'check' ,type: 'success',placement: 'center',time:'2000'}).show();
			 			SysUserListPage.findSysUserList();
			 			$(".closeWindowBtn").click();
			 			return;
			 		}
			 		
			 		new $.zui.Messager(data.message, {icon:'times',type: 'danger',placement: 'center',time:'2000'}).show();
			 		
			 	}		
			 });
	    	
	    },
	    
	    //弹出新增窗口
	    popAddSysUserWindow : function(){
	    	
	    	//时间空间默认为当前时间
	     	$('#addSysUserWindow').find(".form-datetime").val(CommonUtil.getCurrentTimeYmdhms());
	     	
	     	//显示弹窗
	     	$('#addSysUserWindow').modal();
	     	
	    },
	    
	    //获取新增的表单参数
	    getAddSysUserFormParams : function(){
	    	
	    	var params = {};
	    	
			params.account =  $.trim($("#add_account").val());
			params.password =  $.trim($("#add_password").val());
			params.confirmPassword =  $.trim($("#add_confirm_password").val());
			params.userName =  $.trim($("#add_userName").val());
			params.email =  $.trim($("#add_email").val());
			params.phoneNumber =  $.trim($("#add_phoneNumber").val());
			params.gender =  $.trim($("#add_gender").val());
			params.birthday =  $.trim($("#add_birthday").val());
			params.roleIds = $("#addSysUserWindow").find(".roleSelect").val();
	    	
	    	return params;
	    	
	    },
	    
	    //校验新增的参数
	    validateAddSysUserFormParams : function(params){
	    	
	    	if(params.account == ""){
	    		new $.zui.Messager('账号不能为空', {icon:'times',type: 'danger',placement: 'center',time:'2000'}).show();
	    		return;
	    	}
	    	
	    	if(params.password == ""){
	    		new $.zui.Messager('密码不能为空', {icon:'times',type: 'danger',placement: 'center',time:'2000'}).show();
	    		return;
	    	}
	    	
	    	if(params.confirmPassword == ""){
	    		new $.zui.Messager('确认密码不能为空', {icon:'times',type: 'danger',placement: 'center',time:'2000'}).show();
	    		return;
	    	}
	    	
	    	if(params.password != params.confirmPassword ) {
	    		new $.zui.Messager('确认密码不一致', {icon:'times',type: 'danger',placement: 'center',time:'2000'}).show();
	    		return;
	    	}
	    	
	    	if(params.userName == ""){
	    		new $.zui.Messager('姓名不能为空', {icon:'times',type: 'danger',placement: 'center',time:'2000'}).show();
	    		return;
	    	}
	    	
	    	//校验的逻辑自己填写,返回true和false,可以弹出一些提示
	    	return true;

	    },
	    
	    //新增
	    addSysUser : function(){
	    	
	    	var params = this.getAddSysUserFormParams();
	    	
	    	//校验不通过,直接返回
	    	if(!this.validateAddSysUserFormParams(params)) return;
	    	
	    	params.password =  hex_md5(params.password);
	    		
	    	$.ajax({
			 	url : '/sysUser/addSysUser',
			 	type :'POST',
			 	data : JSON.stringify(params),
			 	
			 	beforeSend : function(){
			 		$("#addSysUserBtn").attr("disabled","disabled");
			 	},
			 	complete: function(){
			 		$("#addSysUserBtn").removeAttr("disabled","disabled");
			 	},
			 	dataType : "json",
			 	contentType : 'application/json; charset=UTF-8',
			 	success:function(data){
			 		
			 		if(data.code == 200){
			 			new $.zui.Messager('新增成功!', {icon:'check' ,type: 'success',placement: 'center',time:'2000'}).show();
			 			SysUserListPage.findSysUserList();
			 			$(".closeWindowBtn").click();
			 			return;
			 		}
			 		
			 		new $.zui.Messager(data.message, {icon:'times',type: 'danger',placement: 'center',time:'2000'}).show();
			 		
			 	}		
			 });
	    	
	    },
	    
	    //重置密码弹窗
	    resetPasswordSysUserWindow : function(index){
	    	
	    	//当前编辑对象
	    	this.currentUpdateRecord = this.dataList[index];
	    	
	    	var currentUpdateRecord = this.currentUpdateRecord;
	    	
	     	$('#resetPasswordSysUserWindow').modal();
	    },
	    
	    //重置密码
	    resetPassword : function(){
	       	
	    	//当前编辑对象
	    	var currentUpdateRecord = this.currentUpdateRecord;
	    	
	    	$.ajax({
			 	url : '/sysUser/resetPassword',
			 	type :'POST',
			 	data : {"id" : currentUpdateRecord.id},
			 	dataType : "json",
			 	contentType : 'application/x-www-form-urlencoded; charset=UTF-8',
			 	success:function(data){
			 		
			 		if(data.code == 200){
			 			new $.zui.Messager('重置密码成功!', {icon:'check' ,type: 'success',placement: 'center',time:'2000'}).show();
			 			//删除或更新后停留在当前页
			 			SysUserListPage.pagingTooler.freshPageByDelete();
			 			$(".closeWindowBtn").click();
			 			return;
			 		}
			 		
			 		new $.zui.Messager(''+data.message, {icon:'times',type: 'danger',placement: 'center',time:'2000'}).show();
			 		
			 	}		
			 });
	    	
	    },
	    
	    //弹出菜单权限窗口
	    popMenuRoleWindow : function(index){
	    	
	    	//当前编辑对象
	    	this.currentEidtMenuRecord = this.dataList[index];
	    	
	    	if(this.currentEidtMenuRecord.sysRoleId){
	    		$("#update_userRole").val(this.currentEidtMenuRecord.sysRoleId);
	    	}else{
	    		$("#update_userRole").val("");
	    	}
	    	
	     	$('#menuRoleWindow').modal();
	    
	    },
	    
	    //修改
	    updateUserRole : function(){
	    	
	    	var params = {"id" : this.currentEidtMenuRecord.id ,"sysRoleId" : $("#update_userRole").val()};
	    	
	    	$.ajax({
			 	url : '/sysUser/modifySysUserRole',
			 	type :'POST',
			 	data : JSON.stringify(params),
			 	beforeSend : function(){
			 		$("#updateSysUserRoleBtn").attr("disabled","disabled");
			 	},
			 	complete: function(){
			 		$("#updateSysUserRoleBtn").removeAttr("disabled","disabled");
			 	},
			 	dataType : "json",
			 	contentType : 'application/json; charset=UTF-8',
			 	success:function(data){
			 		
			 		if(data.code == 200){
			 			new $.zui.Messager('保存成功!', {icon:'check' ,type: 'success',placement: 'center',time:'2000'}).show();
			 			//删除或更新后停留在当前页
			 			SysUserListPage.pagingTooler.freshPageByDelete();
			 			$(".closeWindowBtn").click();
			 			return;
			 		}
			 		
			 		new $.zui.Messager(''+data.message, {icon:'times',type: 'danger',placement: 'center',time:'2000'}).show();
			 		
			 	}		
			 });
	    	
	    },
	    
	    //弹出详情窗口
	    popDetailSysUserWindow : function(index){
	    	
	    	//当前编辑对象
	    	this.currentDetailRecord = this.dataList[index];
	    	
	    	var currentDetailRecord = this.currentDetailRecord;
	    	
	    	
	    	for(var key in currentDetailRecord){
	    		
	    		var value = currentDetailRecord[key];
	    		
	    		if(key.lastIndexOf("Time") != -1){
	    			value = CommonUtil.getDateYmdhms(new Date(value));
	    		}
	    		
	    		$("#detail_" + key).val(value);
	    	}
	    	
	    	$('#detailSysUserWindow').modal();
	    },
	    
	    //弹出删除窗口
	    popDeleteSysUserWindow : function(index){
	     	
	    	//当前删除对象
	    	this.currentDeleteRecord = this.dataList[index];
	    	
	    	$('#deleteSysUserWindow').modal();
	    },
	    
	    //删除
	    deleteSysUser : function(){
	    	
	    	var params = {id : this.currentDeleteRecord.id};
	    	
	    	$.ajax({
			 	url : '/sysUser/deleteSysUserById',
			 	type :'POST',
			 	data : params,
			 	beforeSend : function(){
			 		$("#deleteSysUserBtn").attr("disabled","disabled");
			 	},
			 	complete: function(){
			 		$(".closeWindowBtn").click();
			 		$("#deleteSysUserBtn").removeAttr("disabled","disabled");
			 	},
			 	dataType : "json",
			 	contentType : 'application/x-www-form-urlencoded; charset=UTF-8',
			 	success:function(data){
			 		
			 		if(data.code == 200){
			 			new $.zui.Messager('删除成功!', {icon:'check' ,type: 'success',placement: 'center',time:'2000'}).show();
			 			//删除或更新后停留在当前页
			 			SysUserListPage.pagingTooler.freshPageByDelete();
			 			return;
			 		}
			 		
			 		new $.zui.Messager(''+data.message, {icon:'times',type: 'danger',placement: 'center',time:'2000'}).show();
			 		
			 	}		
			 });
	    	
	    }
	    
}

$(function(){
	
	//初始化
	SysUserListPage.init();
	
	//初始化表格
	SysUserListPage.pagingTooler.initTableList();
	
});
